#include "WindowHelper.h"
#include "D3D11.h"

LRESULT CALLBACK WindowProc(HWND hWnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow){
    HWND hWnd;
    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpszClassName = L"DX11";

    RegisterClassEx(&wc);

    hWnd = CreateWindowEx(NULL,
                          L"DX11",    // name of the window class
                          L"DirectX 11",   // title of the window
#ifdef BORDERLESS_WINDOW
						  WS_POPUP,
#endif
#ifndef BORDERLESS_WINDOW
                          WS_OVERLAPPEDWINDOW,    // window style
#endif
						  WH::gdwWindowXPos,    // x-position of the window
						  WH::gdwWindowYPos,    // y-position of the window
#ifdef BORDERLESS_WINDOW
						  WH::gdwWindowWidth,
						  WH::gdwWindowHeight,
#endif
#ifndef BORDERLESS_WINDOW
						  WINDOW_WIDTH,    // width of the window
						  WINDOW_HEIGHT,    // height of the window
#endif
                          NULL,    // we have no parent window, NULL
                          NULL,    // we aren't using menus, NULL
                          hInstance,    // application handle
                          NULL);    // used with multiple windows, NULL

#ifdef BORDERLESS_WINDOW
	HRGN hrgn = CreateRectRgn( 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT );	
	SetWindowRgn( hWnd, hrgn, TRUE );
#endif

    ShowWindow(hWnd, nCmdShow);

	D3D11* pD3D11 = D3D11::Get();
	pD3D11->Init(hWnd);


	MSG msg = {0};

	while(TRUE){
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
			if(msg.message == WM_QUIT)
				break;
		}else{
			// Game code
		}
    }

	pD3D11->Clean();

    return msg.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
    switch(message){
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
    }
    return DefWindowProc (hWnd, message, wParam, lParam);
}